package com.jckl.foodathon.services.config;

import java.io.IOException;

import org.restlet.engine.util.Base64;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jckl.foodathon.common.constant.CommunicationConstants;
import com.jckl.foodathon.common.utils.BitmapEncode;
import com.jckl.foodathon.model.Blogger;
import com.jckl.foodathon.sqlite.dao.DaoSession;
import com.jckl.foodathon.sqlite.model.TB_Blogger;
import com.jckl.foodathon.sqlite.services.BloggerRepository;
import com.jckl.foodathon.sqlite.services.SqliteCommon;

public class GetAllBloggerInfo {
	
	public void perform(Activity activity){

		PerformGetAllBloggerAsync async = new PerformGetAllBloggerAsync(activity);
		async.execute();
	}
	
	private class PerformGetAllBloggerAsync extends AsyncTask<Void, Void, Boolean> {
		
		Context context;
		
		public PerformGetAllBloggerAsync(Activity activity){
			this.context = activity.getApplicationContext();
		}
		
		
		@Override
		protected void onPreExecute() {
			
		}
		
		@Override
		protected Boolean doInBackground(Void... nothing) {
			
			boolean operationComplete = false;
			
			try {
				Gson gson = new Gson();

				ClientResource cr = new ClientResource(CommunicationConstants.SERVER_URI + CommunicationConstants.DATA_BLOGGER);
				Representation rep = cr.get();
				String responseStatus = rep.getText();
				
				//Receive data from server
				Blogger[] bloggers = gson.fromJson(responseStatus, Blogger[].class);
				
				//Insert the data into sqlite
				SqliteCommon sqliteCommon = new SqliteCommon();
				DaoSession session = sqliteCommon.setupDatabase(context);
				//Clear all data in sqlite
				BloggerRepository.clearBloggers(session);
				for (Blogger blogger : bloggers) {
					if(blogger.isActive()){
						TB_Blogger tb_blogger = new TB_Blogger();
						tb_blogger.setName(blogger.getName());
						tb_blogger.setBlogURL(blogger.getBlogURL());
						tb_blogger.setAurthor(blogger.getAuthor());
						if(blogger.getEncodedImage() != null){
							tb_blogger.setImage(Base64.decode(blogger.getEncodedImage()));
						}
						BloggerRepository.insertOrUpdate(session, tb_blogger);
					}
				}
				
				operationComplete = true;	
				
			} catch (IOException IOex) {
				Log.e(CommunicationConstants.APP_TAG, "Unable to parse return value");
				IOex.printStackTrace();
			} catch (Exception ex){
				ex.printStackTrace();
			}
			
			return operationComplete;
		}
		
		@Override
		protected void onProgressUpdate(Void... nothing){
	    }

		
		@Override
		protected void onPostExecute(Boolean responseSuccess) {
			if(responseSuccess){
				Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(context, "Error: ", Toast.LENGTH_SHORT).show();
			}
	    }
	}

}
